package com.sh.链表;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:
 */
public class 反转链表 {
    public static void main(String[] args) {

    }

    public ListNode reverseList(ListNode head) {
        /**
         * 1、设置快慢指针   慢指针就是null   快指针在头节点
         * 2、循环链表，
         *    - 记录快指针的下一个节点      temp
         *    - 快指针的next指向慢指针     fast.next = slow
         *    - 慢指针移动到快指针         slow = fast;
         *    - 快指针后移                fast = temp;
         */
        if (head == null || head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = null;
        while (fast.next != null) {
            ListNode temp = fast.next;
            fast.next = slow;
            slow = fast;
            fast = temp;
        }
        return fast;
    }
}

